package com.lsq.oss.config;

import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Configuration;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;

import java.net.URI;
import java.time.Duration;


@Slf4j
public class MyOssClient {

    private MyOssProperties myOssProperties;


    public MyOssClient(MyOssProperties myOssProperties) {
        this.myOssProperties = myOssProperties;
    }

    /**
     * 创建bucket
     * @param bucketName
     * @return
     */
    public boolean createBucket(String bucketName){
        S3Client s3Client = getS3Client(myOssProperties);
        if(log.isDebugEnabled()){
            log.debug("bucketName :{}",bucketName);
        }
        try {
            s3Client.createBucket(CreateBucketRequest.builder().bucket(bucketName)
                    .build());
        }catch (S3Exception e){
            log.error("创建bucket桶失败",e);
            return false;
        }finally {
            s3Client.close();
        }
        return true;
    }

    public String uploadObject(String bucketName,String objectPath,byte[] bytes){

        S3Client s3Client = getS3Client(myOssProperties);
        PutObjectResponse putObjectResponse = s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(objectPath).build(), RequestBody.fromBytes(bytes));

        return "";

    }

    /**
     * 方式 2：生成预签名 URL（适用于私有对象，可设置过期时间）
     * 即使对象是私有的，持有该 URL 也能在有效期内访问
     */
    public String getPresignedObjectUrl(String bucketName, String objectKey) {
        // 使用 S3Presigner 生成预签名 URL
        try{
            S3Presigner s3Presigner = S3Singleton.getInstance().getS3Presigner(myOssProperties);
            // 构建获取对象的请求
            GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .build();

            // 配置预签名请求（设置过期时间）
            GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
                    .getObjectRequest(getObjectRequest)
                    .signatureDuration(Duration.ofSeconds(myOssProperties.getExpireTime())) // 过期时间（如 Duration.ofMinutes(30) 表示30分钟）
                    .build();

            // 生成预签名 URL
            PresignedGetObjectRequest presignedRequest = s3Presigner.presignGetObject(presignRequest);
            return presignedRequest.url().toString();

        } catch (Exception e) {
            throw new RuntimeException("生成预签名 URL 失败：" + e.getMessage());
        }
    }




    private S3Client getS3Client(MyOssProperties myOssProperties) {
        return S3Singleton.getInstance().getS3Client(myOssProperties);
    }




}
